<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function readSomething($filename) {
    $file_handle = fopen($filename, "r");
    while (!feof($file_handle)) {
        $line = fgets($file_handle);
        echo $line;
    }
    fclose($file_handle);
}

/**
 * Reads the given filename line by line and puts the values (seperated by the given delimiter) in an array. Each
 * index or row has the number of values per line in the given filename
 * @param type $filename
 * @param type $delimiter
 * @return type $array
 */
function readFileIntoArray($filename, $delimiter) {
    $row = 0;
    $array = array();
    if (($handle = fopen($filename, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
            $num = count($data);
            // echo "<p> $num fields in line $row: <br /></p>\n";
            $array[$row] = $data;
            $row++;
//            for ($c = 0; $c < $num; $c++) {
//                echo $data[$c] . "<br />\n";
//            }
        }
        fclose($handle);
    }
    return $array;
}

function readUniqueValuesFromFileIntoArray($filename, $delimiter, $index) {
    $row = 0;
    $array = array();
    if (($handle = fopen($filename, "r")) !== FALSE) {
        while (($data = fgetcsv($handle, 1000, $delimiter)) !== FALSE) {
            $isDuplicate = false;
            $num = count($data);
            // echo "<p> $num fields in line $row: <br /></p>\n";
            //iterate the array and if this a unique value then insert it
            for ($i = 0; $i < sizeof($array); $i++) {
                if (strcmp($data[$index], $array[$i]) === 0) {
                    $isDuplicate = true;
                    break;
                }
            }
            if (!$isDuplicate) {
                $array[$row] = $data[$index];
                $row++;
            }
//            for ($c = 0; $c < $num; $c++) {
//                echo $data[$c] . "<br />\n";
//            }
        }
        fclose($handle);
    }
    return $array;
}

function printNormalArray($array) {
    $arraySize = sizeof($array);
    echo ' array size: ' . $arraySize . '<br>';
    for ($i = 0; $i < $arraySize; $i++) {
        echo $array[$i];        
        echo '<br>';
    }
}
function printArray($array) {
    $arraySize = sizeof($array);
    echo 'array size: ' . $arraySize . '<br>';
    for ($i = 0; $i < $arraySize; $i++) {
        $rowSize = sizeof($array[$i]);
        $row = $array[$i];
        for ($j = 0; $j < $rowSize; $j++) {
            echo ' [' . $row[$j] . '] ';
        }
        echo '<br>';
    }
}

?>
